package com.jci.vsd.activity;

import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.jci.vsd.R;
import com.jci.vsd.activity.enterprise.RegisterCompanyActivity;

import com.jci.vsd.bean.UserBean;
import com.jci.vsd.bean.register.RegisterRequestBean;
import com.jci.vsd.bean.register.RegisterResponseBean;
import com.jci.vsd.constant.AppConstant;
import com.jci.vsd.constant.MySpEdit;
import com.jci.vsd.data.UserData;
import com.jci.vsd.network.control.RegisterControl;
import com.jci.vsd.observer.CommonDialogObserver;
import com.jci.vsd.observer.RxHelper;
import com.jci.vsd.utils.CertServiceUrl;
import com.jci.vsd.utils.Loger;
import com.jci.vsd.utils.PatternMatchUtils;
import com.jci.vsd.view.widget.SimpleToast;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.Map;

import cn.com.syan.jcee.common.sdk.utils.CertificateConverter;
import cn.com.syan.online.sdk.OnlineApplyResponse;
import cn.com.syan.online.sdk.OnlineClient;
import cn.com.syan.online.sdk.OnlineIssueResponse;
import cn.unitid.spark.cm.sdk.business.*;


import butterknife.BindView;
import cn.unitid.spark.cm.sdk.exception.CmSdkException;
import cn.unitid.spark.cm.sdk.listener.ProcessListener;
import io.reactivex.Observable;

/**
 * Created by liqing on 18/6/25.
 * 注册
 */

public class RegisterActivity extends BaseActivity {

    //    @BindView(R.id.edt_acount)
//    EditText edtAccount;
    @BindView(R.id.edt_real_name)
    EditText edtName;
    @BindView(R.id.edt_id)
    EditText edtID;
    @BindView(R.id.edt_tel)
    EditText edtTel;
    @BindView(R.id.btn_register)
    Button btnRegister;
    @BindView(R.id.edt_psw)
    EditText edtPsw;
    @BindView(R.id.check_agree)
    CheckBox checkAgree;
    @BindView(R.id.edt_emil)
    EditText edtEmail;
    @BindView(R.id.tv_agree)
    TextView tvAgree;

    private boolean agree = false; //协议同意，
    private OnlineClient onlineClient;//申请证书客户端
    private String vcode;//手机验证码（后期开通）
    private String account;//账号
    private String psw;
    private String realName;
    private String id;
    private String tel;
    private CBSCertificateStore store = null;//证书存储
    private String email;
    private MySpEdit prefs;
    private String idNO; //身份证
    private String spRealName; //保存的真实姓名
    private boolean hasCert = false;//是否有证书


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        prefs = MySpEdit.getInstance();
        tvAgree.setText(Html.fromHtml(getResources().getString(R.string.str_agree, 1, "", "协议条款")));
        initViewEvent();
        onlineClient = new OnlineClient(CertServiceUrl.baseUrl, CertServiceUrl.appKey, CertServiceUrl.appSecret);
        checkAgree.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {


            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                agree = isChecked;

            }
        });
    }

    //检测是否有证书

    public boolean checkCer() {

        prefs = MySpEdit.getInstance();

        String realName = prefs.getRealName();
        String idNO = prefs.getIDNo();
        String phone = prefs.getUser();

        if (TextUtils.isEmpty(realName) || TextUtils.isEmpty(idNO) || TextUtils.isEmpty(phone)) {
            SimpleToast.ToastMessage("请先申请个人证书！");
            toActivity(RegisterCertActivity.class);
            finish();

        } else {
            edtName.setText(prefs.getRealName());
            edtID.setText(prefs.getIDNo());
            edtTel.setText(prefs.getUser());
        }
        return hasCert;
    }


    @Override
    protected void initViewEvent() {
        btnRegister.setOnClickListener(this);
        tvAgree.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view.getId() == R.id.btn_register) {
            //去注册
            if (agree) {
                checkData();
            } else {
                Toast.makeText(RegisterActivity.this,
                        " 请先同意协议条款", Toast.LENGTH_SHORT).show();
            }
        } else if (view.getId() == R.id.tv_agree) {
            //协议显示
            showAgreeCustomDialog();
        }
    }


    //协议条款
    private void showAgreeCustomDialog() {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = LayoutInflater.from(this);
        View v = inflater.inflate(R.layout.dialog_agree, null);
        TextView tv_close = (TextView) v.findViewById(R.id.tv_close);

        //builer.setView(v);//这里如果使用builer.setView(v)，自定义布局只会覆盖title和button之间的那部分
        final Dialog dialog = builder.create();
        dialog.show();

        dialog.getWindow().setContentView(v);
        dialog.getWindow().setGravity(Gravity.CENTER);//可以设置显示的位置

        tv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

//        new AlertDialog.Builder(this)
//                .setTitle(R.string.agree_title).setView(sc)
//                .create().show();
    }

    private void initTestDataLq() {
        // edtAccount.setText("lq");
        edtPsw.setText("123456");
        edtName.setText("lq");
        edtID.setText("320322199007171428");
        edtTel.setText("15951882547");
        edtEmail.setText("2319345023@qq.com");

    }

    private void initTestDataDingWei() {
        //   edtAccount.setText("丁薇");
        edtPsw.setText("123456");
        edtName.setText("丁薇");
        edtID.setText("320682199610101769");
        edtTel.setText("13770930779");
        edtEmail.setText("1484951857@qq.com");

    }

    private void initTestDataT() {
        // edtAccount.setText("屠正松");
        edtPsw.setText("123456");
        edtName.setText("屠正松");
        edtID.setText("320113199310156418");
        edtTel.setText("15252466554");
        edtEmail.setText("2319345023@qq.com");

    }


    private void initTestData2() {
        //  edtAccount.setText("屠正松");
        edtPsw.setText("123456");
        edtName.setText("张睿");
        edtID.setText("321027199409053017");
        edtTel.setText("13773506614");
        edtEmail.setText("2319345023@qq.com");

    }


    private void initTestData3() {
        //  edtAccount.setText("屠正松");
        edtPsw.setText("123456");
        edtName.setText("周先康");
        edtID.setText("342601199406205015");
        edtTel.setText("15951883214");
        edtEmail.setText("2319345023@qq.com");

    }

    private void initTestData4() {
        //  edtAccount.setText("屠正松");
        edtPsw.setText("123456");
        edtName.setText("王军刚");
        edtID.setText("321023199401295411");
        edtTel.setText("15161858368");
        edtEmail.setText("2319345023@qq.com");

    }

    //张琳     321121199208092922       15805152893
    private void initTestData5() {
        //  edtAccount.setText("屠正松");
        edtPsw.setText("123456");
        edtName.setText("张琳");
        edtID.setText(" 321121199208092922");
        edtTel.setText("15805152893");
        edtEmail.setText("2319345023@qq.com");

    }

    private void checkData() {

        //  account = edtAccount.getText().toString().trim();
        psw = edtPsw.getText().toString().trim();
        realName = edtName.getText().toString().trim();
        id = edtID.getText().toString();
        tel = edtTel.getText().toString();
        email = edtEmail.getText().toString();

//        if (TextUtils.isEmpty(account)) {
//            Toast.makeText(RegisterActivity.this, "请输入账号", Toast.LENGTH_SHORT).show();
//            edtAccount.requestFocus();
//            return;
//        }

        if (TextUtils.isEmpty(psw)) {
            Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
            edtPsw.requestFocus();
            return;
        }

        if (TextUtils.isEmpty(realName)) {
            Toast.makeText(RegisterActivity.this, "请输入姓名", Toast.LENGTH_SHORT).show();
            edtName.requestFocus();
            return;
        }

//        if (!TextUtils.isEmpty(spRealName)) {
//            if (!realName.equals(spRealName)) {
//                Toast.makeText(RegisterActivity.this, "请填写申请证书的姓名", Toast.LENGTH_SHORT).show();
//                edtName.requestFocus();
//                return;
//            }
//        }

//        if (TextUtils.isEmpty(id)) {
//            Toast.makeText(RegisterActivity.this, "请输入身份证号", Toast.LENGTH_SHORT).show();
//            edtID.requestFocus();
//            return;
//        }

//        if (!TextUtils.isEmpty(idNO)) {
//            if (!id.equals(idNO)) {
//                Toast.makeText(RegisterActivity.this, "请填写申请证书的身份证号", Toast.LENGTH_SHORT).show();
//                edtID.requestFocus();
//                return;
//            }
//        }


        if (TextUtils.isEmpty(tel)) {
            Toast.makeText(RegisterActivity.this, "请输入手机号码", Toast.LENGTH_SHORT).show();
            edtTel.requestFocus();
            return;
        } else if (!PatternMatchUtils.isMobile(tel)) {
            Toast.makeText(RegisterActivity.this, "手机号码不合法", Toast.LENGTH_SHORT).show();
            return;
        }

//        if (TextUtils.isEmpty(email)) {
//            Toast.makeText(RegisterActivity.this, "请输入邮箱", Toast.LENGTH_SHORT).show();
//            edtEmail.requestFocus();
//            return;
//        } else if (!PatternMatchUtils.isEmail(email))
        //  vcode = "1234";
//                if ("".equals(vcode)) {
//                    ToastUtil.showToast(RegFirstStepActivity.this, "请输入验证码", Toast.LENGTH_SHORT);
//                    yzmet.requestFocus();
//                    return;
//                }

        //toActivity(MainActivity.class);
        //   goRegisterCertify();
        //注册证书 －－》同步证书

        goRegister();
    }

//    private void goRegisterCertify() {
//        CertificateRegisterService certificateRegisterService = new CertificateRegisterService(RegisterActivity.this,
//                onlineClient, new ProcessListener<OnlineApplyResponse>() {
//            @Override
//            public void doFinish(OnlineApplyResponse onlineApplyResponse, String cert) {
//
//                CertificateIssueService certificateIssueService = new CertificateIssueService(RegisterActivity.this, onlineClient, new ProcessListener<OnlineIssueResponse>() {
//                    @Override
//                    public void doFinish(OnlineIssueResponse data, String cert) {
//                        Log.e("CertificateIssueService", "onFinish");
//
//                        String your_signature = data.getSignCert();
//                        String your_encryption = data.getEncCert();
//
//                        X509Certificate x509Certificate = null;
//                        try {
//                            x509Certificate = CertificateConverter.fromBase64(your_signature);
//
//                        } catch (CertificateException e) {
//                            e.printStackTrace();
//                        }
//
//                        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//                        String validate = formatter.format(x509Certificate.getNotBefore()) + " - " +
//                                formatter.format(x509Certificate.getNotAfter());
//                        //注册
//                        goRegister();
//
//
//                    }
//
//                    @Override
//                    public void doException(CmSdkException exception) {
////                        if (pdu.getMypDialog().isShowing()) {
////                            pdu.dismisspd();
////                        }
//                        Log.e("CertificateIssueService", "doException");
//                        Toast.makeText(RegisterActivity.this, "" + exception.getMessage(), Toast.LENGTH_SHORT).show();
//                        Log.e("firstRegister", "showException" + exception.getMessage());
//                    }
//                });
//                String exts = onlineApplyResponse.getExtensions();
//                Gson gson = new GsonBuilder().disableHtmlEscaping().create();
//                Map<String, String> retMap = gson.fromJson(exts, new TypeToken<Map<String, String>>() {
//                }.getType());
//
//                certificateIssueService.issue(onlineApplyResponse.getApplyId(), onlineApplyResponse.getSubject(), retMap, onlineApplyResponse.getAlgorithm(), true);
//
//
//            }
//
//
//            @Override
//            public void doException(CmSdkException e) {
////
////                if (pdu.getMypDialog().isShowing()) {
////                    pdu.dismisspd();
////                }
//                Toast.makeText(RegisterActivity.this, "" + e.getMessage(), Toast.LENGTH_SHORT);
//                Log.e("RegisterActivity", "CmSdkException" + e.getMessage());
//
//            }
//        });
//
//        try {
//
//            certificateRegisterService.register(realName, id, tel, "1234", null);
//        } catch (Exception e) {
//            Toast.makeText(RegisterActivity.this, "" + e.getMessage(), Toast.LENGTH_SHORT);
//            e.printStackTrace();
//        }
//
//
//    }

    private void goRegister() {
        final RegisterRequestBean requestBean = new RegisterRequestBean();
        requestBean.setRealname(realName);
        requestBean.setIdNumber(id);
        requestBean.setPhone(tel);
        requestBean.setEmail(email);
        requestBean.setPassword(psw);
//        requestBean.setType(type);

        Observable<RegisterResponseBean> observable = new RegisterControl()
                .register(requestBean);
        CommonDialogObserver<RegisterResponseBean> observer = new CommonDialogObserver<RegisterResponseBean>(this) {
            @Override
            public void onNext(RegisterResponseBean registerResponseBean) {
                super.onNext(registerResponseBean);
                String status = registerResponseBean.getStatus();
                if (status.equals("403")) {
                    SimpleToast.toastMessage("已注册", Toast.LENGTH_SHORT);
                }
                if (status.equals("200")) {
                    Loger.e("--uid=" + registerResponseBean.getId());
                    SimpleToast.toastMessage(" 注册成功", Toast.LENGTH_SHORT);

                }
                //(code == 20005 -- 201
                if (status.equals("205")) {
                    SimpleToast.toastMessage(" 帐号已存在,去登录", Toast.LENGTH_LONG);

                }
                //
                if (status.equals("206")) {
                    SimpleToast.toastMessage(" 手机号已注册,去登录", Toast.LENGTH_LONG);
                }

                if (status.equals("207")) {
                    SimpleToast.toastMessage(" 身份证号码已注册,去登录", Toast.LENGTH_LONG);
                }

                if (status.equals("208")) {
                    SimpleToast.toastMessage("账户已注册,去登录", Toast.LENGTH_LONG);
                }

                prefs.setUser(tel);
                prefs.setPsw(psw);

                toActivity(LoginActivity.class);
                finish();
            }

            @Override
            public void onError(Throwable t) {
                super.onError(t);
                SimpleToast.toastMessage(t.getMessage(), Toast.LENGTH_SHORT);
//                if(t().equals("403")){
//                 //   SimpleToast.toastMessage("账户已注册,去登录", Toast.LENGTH_LONG);
//                    prefs.setUser(tel);
//                    prefs.setPsw(psw);
//                    toActivityWithType(LoginActivity.class, intentType);
//                    finish();
//                }
            }
        };
        RxHelper.bindOnUIActivityLifeCycle(observable, observer, this);

    }


}
